#!/bin/sh
BASEDIR=$(dirname "$0")
#echo "$BASEDIR"
#npm install
git submodule update --init --recursive
rm -rf plugins
rm -rf platforms
env='dev'
appId="00015fddefbc75de"
tokeAPIServer="http://120.78.81.170/api/wudang-security/oauth/token"

if [[ $3 == 'test' ]]; then
	env='test'
	appId="000163fc5c5be71f"
	tokeAPIServer="https://weixin.wuyixi.com/api/wudang-security/oauth/token"
elif [[ $3 == 'prod'  ]]; then
	env='prod'
	appId="0001622da8f1c353bb"
	tokeAPIServer="https://app.csg.cn:9093/api/wudang-security/oauth/token"
fi

echo "$env---------$appId-------$tokeAPIServer"


# ionic cordova plugin add ./node_modules/cordova-plugin-enterprisewechat --variable WWECHATAPPID=$appId --variable GETCODEAPI=$tokeAPIServer --variable EWC_SCOPE="e23j3442jp2" --variable EWC_STATE="1d9919f3w7d" --variable SCHEMES="csgZhangShangWuDang"

if [[ $1 == 'ios' ]] ; then
    # if [[ $env  == 'dev' ]] ; then
    #     cp -rf  ./plugins/cordova-plugin-enterprisewechat/src/ios/libs/eim/EIMSocialBundle.bundle ./plugins/cordova-plugin-enterprisewechat/src/ios/libs/EIMSocialBundle.bundle
    # else
    #     cp -rf ./plugins/cordova-plugin-enterprisewechat/src/ios/libs/nw/EIMSocialBundle.bundle ./plugins/cordova-plugin-enterprisewechat/src/ios/libs/EIMSocialBundle.bundle
    # fi
    ionic cordova platform add ios@4.5.2

	if [[ $2 == 'release' ]]; then
		export NODE_ENV=$env && ionic cordova build ios --device --minifycss --minfyjs --optimizejs  --release --buildConfig "$BASEDIR/build.json"
	else
		export NODE_ENV=$env && ionic cordova build ios --device --release --buildConfig "$BASEDIR/build.json"
	fi
fi

if [[ $1 == 'android' ]] ; then
    # if [[ $env  == 'dev' ]] ; then
    #     cp -rf  ./plugins/cordova-plugin-enterprisewechat/src/android/eim/eim-auth-sdk-1.0.5.jar  ./plugins/cordova-plugin-enterprisewechat/src/android/eim-auth-sdk-1.0.5.jar
    # else
    #     cp -rf ./plugins/cordova-plugin-enterprisewechat/src/android/csg/eim-auth-sdk-1.0.5.jar  ./plugins/cordova-plugin-enterprisewechat/src/android/eim-auth-sdk-1.0.5.jar
    # fi
	ionic cordova platform add android@6.4.0
    cp $BASEDIR/build-extras.gradle $BASEDIR/../platforms/android/build-extras.gradle
	#TODO
	if [[ $2 == 'release' ]]; then
		export NODE_ENV=$env && ionic cordova build android --device --minifycss --minfyjs --optimizejs --prod --release
	else
		export NODE_ENV=$env && ionic cordova build android
	fi
fi

